package com.itheima.core.web.controller;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.itheima.common.utils.Page;
import com.itheima.core.po.BaseDict;
import com.itheima.core.po.Customer;
import com.itheima.core.po.User;
import com.itheima.core.service.UserService;
/**
 * 用户控制器类
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;
	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String usercode,String password, Model model, 
                                                          HttpSession session) {
		// 通过账号和密码查询用户
		User user = userService.findUser(usercode, password);
		if(user != null){		
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			// 跳转到主页面
//			return "customer";
			return "redirect:customer/list.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
         // 返回到登录页面
		return "login";
	}
	
	/**
	 * 模拟其他类中跳转到客户管理页面的方法
	 */
	@RequestMapping(value = "/toCustomer.action")
	public String toCustomer() {
	    return "customer";
	}
	/**
	 * 模拟其他类中跳转到用户管理页面的方法
	 */
	@RequestMapping(value = "/toUser.action")
	public String toUser() {
	    return "user";
	}
	
	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
	    // 清除Session
		/* session.invalidate(); */
	    // 重定向到登录页面的跳转方法
	    return "redirect:login.action";
	}
	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
	    return "login";
	}
	/**
	 * 向用户注册页面跳转
	 */
	@RequestMapping(value = "/toRegister.action", method = RequestMethod.POST)
	public String toRegister( ) {
		return "注册界面";
	}
	/**
	 * 接收用户注册信息
	 * @throws IOException 
	 */
	@RequestMapping(value = "/registerUser.action")
	public String registerUser(@RequestParam("user_code") String user_code,
			@RequestParam("user_name") String user_name,
			@RequestParam("user_password") String user_password,
			@RequestParam("user_state") Integer user_state)  {
		User user = new User();
		user.setUser_code(user_code);
		user.setUser_name(user_name);
		user.setUser_password(user_password);
		user.setUser_state(user_state);
		userService.createUser(user);
	    System.out.println("user_code="+user_code);
	    System.out.println("user_name="+user_name);
	    System.out.println("user_password="+user_password);
	    System.out.println("user_state="+user_state);
	    
	    return "login";
	}
	
}
